package cn.siat.gene.domain;

import com.alibaba.fastjson.JSON;
import cn.siat.gene.model.Location;
import cn.siat.gene.util.GenBankToJson;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Data
@Accessors(chain = true)
public class Feature implements Serializable {
    private static final long serialVersionUID = 3742296796418883453L;
    private Long id;
    private Long idCopy;
    public Long getIdCopy(){
        if(idCopy==null){
            return id;
        }
        return idCopy;
    }
    private Long sequenceId;
    private Integer start;
    private Integer end;
    private List<Location> locations;
    private Map<String,List<String>> notes;
    /**key:operateId,value:start,end*/
    private Map<String,String> history;
    private String type;
    private Integer strand;
    private String name;
    private String arrowheadType;
    private String sequenceName;
    private String operator;
    private String source;
    /**
     * 是否临时,临时的初始加载会被删除
     */
    private Boolean isTemporary;
    private Integer sort;
    private Integer sequenceLength;
    private Integer oldSequenceLength;
    /**删除标记*/
    private Integer isDel;
    /**绑定的操作id,在回退时,小于等于此id直接删除*/
    private Long operateId;
    public Feature(Long id) {
        this.id = id;
    }

    public Feature() {
    }
    public Feature(Feature feat) {
        // history和location不在这里赋值
        this.name = feat.getName();
        this.start = feat.getStart();
        this.end = feat.getEnd();
        this.id = feat.getId();
        this.sequenceId = feat.getSequenceId();
        this.notes = feat.getNotes();
        this.type = feat.getType();
        this.strand = feat.getStrand();
        this.arrowheadType = feat.getArrowheadType();
        this.sequenceName = feat.getSequenceName();
        this.source = feat.getSource();
        this.isTemporary = feat.getIsTemporary();
        this.sort = feat.getSort();
        this.sequenceLength = feat.getSequenceLength();
    }
    /**
     * py解析后的feature需要处理一下
     */
    public void init(int bpLength){
        if(locations!=null && !locations.isEmpty()){
            this.start = locations.get(0).getStart();
            this.end = locations.get(locations.size()-1).getEnd();
            // py解析后环形的一条会被处理成两条
            for (int i = 0; i < locations.size(); i++) {
                Location location = locations.get(i);
                if(location.getEnd()==bpLength-1 && i!=locations.size()-1&& locations.get(i+1).getStart()==0){
                    location.setEnd(locations.get(i+1).getEnd());
                    locations.remove(i+1);
                    break;
                }
            }
        }
        GenBankToJson.postProcessGenbankFeature(this);
    }
}
